﻿using DevExpress.XtraEditors;
using SX_MesProject.Dtos;
using SX_MesProject.Dtos.LJQDtos;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static SX_MesProject.HttpClientHelper;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraCharts.Native;
using System.Data.SqlClient;
using Newtonsoft.Json;
using SX_MesProject.ProcessManage;
using DevExpress.Xpo;

namespace SX_MesProject
{
    public partial class InventoryLocationManage : DevExpress.XtraEditors.XtraForm
    {
        public InventoryLocationManage()
        {
            InitializeComponent();
        }

        private void gridControl1_Load(object sender, EventArgs e)
        {

            //gridView1
            ////自带搜索框
            //gridView1.OptionsFind.AlwaysVisible = true;
            ////单条选中且不可编辑 
            //gridView1.OptionsBehavior.Editable = false;
            //gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
            gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
            //gridControl1.UseEmbeddedNavigator = true;
            //单击事件
            gridView1.RowClick += GridView_RowClick;

            gridView2.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;

#pragma warning disable CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
            Databinds();
#pragma warning restore CS4014 // 由于此调用不会等待，因此在调用完成前将继续执行当前方法
        }
        private async Task Databinds()
        {
            try
            {
                var list = await HttpClientHelper.RequestUrl<string, ApiResult<List<WorkingProcedureList>>>("https://localhost:44556/", "GetWorkingProcedureList", MethodType.GET);
                this.gridControl1.DataSource = list.Data;

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
        private void GridView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            // 这里是点击行时要执行的逻辑
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            if (e.RowHandle >= 0)
            {
                // 获取点击行的数据
                object rowData = view.GetRow(e.RowHandle);
                // 进行相应的处理
                string rowData1 = JsonConvert.SerializeObject(rowData);
                var list = Newtonsoft.Json.JsonConvert.DeserializeObject<WorkingProcedureList>(rowData1);
                ProcedureBindEquipment(list.Id);
            }
        }
        public int Value { get; set; }
        private async void ProcedureBindEquipment(int procedureId)
        {
            try
            {
                ProcedureBindQueryId procedureBindQueryId = new ProcedureBindQueryId();
                procedureBindQueryId.ProcedureId = procedureId;
                var list = await HttpClientHelper.RequestUrl<ProcedureBindQueryId, ResponseDto<List<ProcedureBindEquipmentDto>>>("https://localhost:44556/", "GetProcedureBindList", MethodType.POST, procedureBindQueryId);
                this.Value = procedureId;
                this.gridControl2.DataSource = list.Data;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            ProcedureAdd procedureAdd = new ProcedureAdd();
            procedureAdd.OnLoadData += Databinds;
            procedureAdd.ShowDialog();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (this.Value <1)
            {
                MessageBox.Show("请先选择需要绑定的工序");
            }
            else
            {
                ProcedureBindAdd procedureBindAdd = new ProcedureBindAdd(this.Value);
                procedureBindAdd.OnLoadData += Databinds;
                procedureBindAdd.ShowDialog();
            }
        }
    }
}